//
//  GYLockViewController.m
//  LockManagement
//
//  Created by apple on 2018/7/21.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import "GYLockViewController.h"
#import "GYLockHeadView.h"
#import "GYLockListViewController.h"
#import "GYLockMapViewController.h"


@interface GYLockViewController ()

/** 头视图 */
@property (nonatomic, strong) GYLockHeadView *lockHeadView;

/** 正在显示的控制器 */
@property (nonatomic, strong) UIViewController *showingController;



@end

@implementation GYLockViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    //这个具体高度 ----> 后期可以优化成 活动的高度(根据约束来修改)
    self.lockHeadView.frame = CGRectMake(0, 0, self.view.width, 120);
}

- (void)setupUI {
    self.view.backgroundColor = [UIColor whiteColor];
   
    
    //1.添加头视图
    GYLockHeadView *lockHeadView = [[[NSBundle mainBundle] loadNibNamed:@"GYLockHeadView" owner:nil options:nil] firstObject];
    __weak typeof(self) weakSelf = self;
    
    //头标图选择器
    lockHeadView.lockHeadSelectCallBack = ^(NSInteger currentIndex) {
        //移除当前显示的View
        [weakSelf.showingController.view removeFromSuperview];
        weakSelf.showingController = weakSelf.childViewControllers[currentIndex-1];
        weakSelf.showingController.view.frame = CGRectMake(0, 120, weakSelf.view.width, weakSelf.view.height-120);
        [weakSelf.view addSubview:weakSelf.showingController.view];
    };
    
    //2.搜索
    lockHeadView.searchBtnClcikCallBack = ^(NSString *searchText){
        NSLog(@"搜索按钮点击事件.....");
        if ([weakSelf.showingController isMemberOfClass:[GYLockListViewController class]]) {
            GYLockListViewController *lockListVC = (GYLockListViewController *)weakSelf.showingController;
            lockListVC.lockName = searchText;
        }
    };
    
    [self.view addSubview:lockHeadView];
    self.lockHeadView = lockHeadView;
    
    //2.初始化子控件
    [self addChildControllers];
}

- (void)addChildControllers {
    //1.添加列表控制器
    GYLockListViewController *lockListController = [[GYLockListViewController alloc] init];
    lockListController.view.frame = CGRectMake(0, 120, self.view.width, self.view.height-120);
    kWeakSelf(self)
//    lockListController.planningRouteCallBack = ^{
//        weakself.lockHeadView.showMap = YES;
//    };
    
    [self.view addSubview:lockListController.view];
    [self addChildViewController:lockListController];
    self.showingController = lockListController;
    
    //添加地图控制器
    GYLockMapViewController *lockMapController = [[GYLockMapViewController alloc] init];
    [self addChildViewController:lockMapController];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

#pragma mark -- BMKMap




#pragma mark -- BMKLocationServiceDelegate


#pragma mark  --- 懒加载


















@end
